package com.jadesoft.ciadapter.plugin;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.ServletRequest;

import com.jadesoft.ciadapter.library.JdPageHelper;
import com.jadesoft.ciadapter.plugin.base.DefaultPageAction;
import com.jadesoft.ciadapter.plugin.base.Pluggable;
import com.jadesoft.ciadapter.plugin.base.PluginAction;
import com.jadesoft.ciadapter.plugin.base.PluginManager;
import com.ufida.leaf.exception.BusinessException;

import freemarker.template.TemplateException;

public class ScreenMetaPlugin implements Pluggable {
	ServletRequest request = null ;
	public void init(Object container) {
		if ( ! (container instanceof ServletRequest)){
			throw new BusinessException("invalid container");
		}
		this.request = (ServletRequest)container ;
		PluginAction screenMeta = new DefaultPageAction(this.request){
			public Object exec(Object... args) {
				return getScreenMeta();
			}
		};
		PluginManager.addAction("before_content", screenMeta,container);

	}
	
	
	private String getScreenMeta(){
		JdPageHelper helper = JdPageHelper.getHelper(this.request);
		String help_msg = helper.getPageMeta("contextual_help");
		HashMap<String,Object> data = new HashMap<String,Object>();
		data.put("help_msg", help_msg);
		
		String screenMeta = "";
		try {
			screenMeta = JdPageHelper.loadTemplate("jd_base/help.ftl", data, null, this.request);
		} catch (TemplateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return screenMeta ;
	}

}
